package org.google.maps;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class Polyline extends Overlay {
	private List<GeoPoint> geoPoints;
	private int color = Color.RED;

	public Polyline(List<GeoPoint> geoPoints) {
		this.geoPoints = geoPoints;
	}
	
	@Override
	public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
		Projection projection = mapView.getProjection();
		if(shadow == false){
			Paint paint = new Paint();
			paint.setAntiAlias(true);
			paint.setColor(this.color);
			paint.setStrokeWidth(2);
			
			float[] fPoints = new float[this.geoPoints.size() * (this.geoPoints.size() - 1)];
			Point point = new Point();
			
			int i=0;
			int j=0;
			for (GeoPoint geoPoint : this.geoPoints) {
				projection.toPixels(geoPoint, point);
				
				fPoints[i++] = (float)point.x;
				fPoints[i++] = (float)point.y;
				
				
				
				if(j > 0 && j < this.geoPoints.size() - 1){
					fPoints[i++] = (float)point.x;
					fPoints[i++] = (float)point.y;
				}
				
				j++;
			}
			canvas.drawLines(fPoints, paint);
		}
		return super.draw(canvas, mapView, shadow, when);
	}

	public void SetColor(int color){
		this.color = color;
	}
	
}
